﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using ncp.Business;
using ncp.Business.Common;

namespace ncp.Web.cms
{
    public partial class Goods_List : System.Web.UI.Page
    {
        public static string mFilePath = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(Goods_List));
            mFilePath = Server.MapPath("../../../uc/headLogo.ascx");
        }

        //装载商品表视图
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string BindTable(string GC_Id)
        {
            DataTable dt = GoodsClassBll.GetGoodsClassTable("GC_Status='1' and (GC_PId='" + GC_Id + "' or gcpid='" + GC_Id + "') and charindex('h',GC_Flag,0)>0", "GC_Id", CommonConst.DB_Name);
            return JsonHelper.DataTableToJSON(dt);
        }

        //发布商品信息
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string ReleaseGoods(string GC_Id)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            try
            {
                string repStart = ""; string repEnd = ""; string strName = "";
                StringBuilder builder=new StringBuilder();
                var dt = GoodsClassBll.GetGoodsClassTable("GC_Status='1' and (GC_PId='" + GC_Id + "' or gcpid='" + GC_Id + "') and charindex('h',GC_Flag,0)>0", "GC_Id", CommonConst.DB_Name);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string str = ((i+1) % 4 == 0) ? "" : "&nbsp;|&nbsp;";
                    if (i == dt.Rows.Count - 1) str = "";
                    if(dt.Rows[i]["GC_Level"].ToString()=="2")
                        builder.AppendFormat("<a href=\"/products/{0}-{1}-{2}-{3}.html\">{4}</a>" + str + "", dt.Rows[i]["GC_PId"].ToString(), dt.Rows[i]["GC_Id"].ToString(), "0", "0", dt.Rows[i]["GC_Name"].ToString());
                    else
                        builder.AppendFormat("<a href=\"/products/{0}-{1}-{2}-{3}.html\">{4}</a>" + str + "", dt.Rows[i]["gcpid"].ToString(), dt.Rows[i]["GC_PId"].ToString(), dt.Rows[i]["GC_Id"].ToString(), "0", dt.Rows[i]["GC_Name"].ToString());
                }
                if (GC_Id == "1")
                {
                    strName = "农品";
                    repStart = "<%--start1--%>";
                    repEnd = "<%--end1--%>";
                    
                }
                else if (GC_Id == "2")
                {
                    strName = "农资";
                    repStart = "<%--start2--%>";
                    repEnd = "<%--end2--%>";
                }
                else
                {
                    strName = "资讯";
                    repStart = "<%--start3--%>";
                    repEnd = "<%--end3--%>";
                }
                //GoodsClassBll.ReleaseGoodsClass(DictList["g_idlist"].ToString(), DictList["g_flag"].ToString(),DictList["g_type"].ToString(), CommonConst.DB_Name);
                CommonBll.ReplaceFileContent(mFilePath, repStart, repEnd, builder.ToString());
                var userInfo = WebMasterCookie.GetCookie(CommonConst.adminCookieName);
                Dictionary<string, object> dicHandleLog = new Dictionary<string, object>();
                dicHandleLog.Add("user_id", userInfo.Id);
                dicHandleLog.Add("computername", userInfo.Computername);
                dicHandleLog.Add("computerip", userInfo.Computerip);
                dicHandleLog.Add("handle_note", "发布" + strName + "到首页");
                dicHandleLog.Add("obj_guid", "GC_Id=" + GC_Id);
                LogHandleBll.AddHandleLog(dicHandleLog, CommonConst.DB_Name);
                jsonResult.Add("msg", "发布成功！");
                jsonResult.Add("isreload", true);
            }
            catch (Exception ex)
            {
                jsonResult.Clear();
                jsonResult.Add("msg", ex.Message);
                jsonResult.Add("isreload", false);
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }


    }
}
